package org.spolo.android.util;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.SoftReference;
import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class MyBitmapFactory {
	private Context context;
	private AssetManager assetManager;
	private Bitmap[] sequencebitmaps;
	private Map<String, SoftReference<Bitmap[]>> imageCacheMap;
	private static MyBitmapFactory instance = null;
	private InputStream inputStream = null;
	private BitmapFactory.Options options = null;

	private MyBitmapFactory(Context con) {
		this.context = con;
		assetManager = this.context.getAssets();
		// 软引用
		imageCacheMap = new HashMap<String, SoftReference<Bitmap[]>>();
		options = new BitmapFactory.Options();
		options.inPurgeable = true;
	}

	public static MyBitmapFactory getInstance(Context con) {
		if (instance == null) {
			return instance = new MyBitmapFactory(con);
		} else {
			return instance;
		}
	}

	/**
	 * @param imgname
	 *            所要读取图片的包名
	 * @return Bitmap[] 返回此套图片的Bitmap数组
	 */
	public void createBitmap(String packagename) {
		long start = System.currentTimeMillis();

		String[] files = null;
		try {
			files = assetManager.list(packagename);

			sequencebitmaps = new Bitmap[files.length];

		} catch (IOException e) {
			e.printStackTrace();
		}
		for (int index = 0; index < files.length; index++) {

			try {
				inputStream = assetManager.open(packagename + File.separator
						+ files[index]);
			} catch (IOException e) {
				e.printStackTrace();
			}
			Bitmap bitmap = BitmapFactory.decodeStream(inputStream, null,
					options);
			sequencebitmaps[index] = bitmap;

		}
		closeInputStream(inputStream);
		SoftReference<Bitmap[]> softreferencebitmaps = new SoftReference<Bitmap[]>(
				sequencebitmaps);
		imageCacheMap.put(packagename, softreferencebitmaps);

		System.out.println("list 耗时" + (System.currentTimeMillis() - start));
	}

	/**
	 * @param packagename
	 *            String 所要读取图片的包名
	 * @param imgname
	 *            String 此套图片除数字编号外的字符串，例如：abc0001.jpg，可以传入abc
	 * @param count
	 *            int 此套图片总张数
	 * @return Bitmap[] 返回此套图片的Bitmap数组
	 */
	public void createBitmap(String packagename, String imgname, int count) {
		long start = System.currentTimeMillis();
		String index = null;
		// options.inSampleSize = 2;
		sequencebitmaps = new Bitmap[count];
		// 格式化文件索引字符串
		String pattern = "0000";
		java.text.DecimalFormat df = new java.text.DecimalFormat(pattern);
		for (int i = 0; i <= count; i++) {
			index = df.format(i);
			try {
				inputStream = assetManager.open(packagename + File.separator
						+ imgname + index + ".jpg");
			} catch (IOException e) {
				e.printStackTrace();
			}
			Bitmap bitmap = BitmapFactory.decodeStream(inputStream, null,
					options);
			sequencebitmaps[i] = bitmap;
		}
		SoftReference<Bitmap[]> softreferencebitmaps = new SoftReference<Bitmap[]>(
				sequencebitmaps);
		imageCacheMap.put(packagename, softreferencebitmaps);
		closeInputStream(inputStream);
		System.out.println("direct read img 耗时"
				+ (System.currentTimeMillis() - start));
	}

	public Bitmap[] getBitmaps(String packagename) {
		// // 首先是从缓存中获取之前的数据
		Bitmap[] images = getImageByCache(packagename);
		if (images != null && images.length > 0) {
			System.out
					.println("MyBitmapFactory-->getBitmaps-->images read by cache");
			// 从缓存中读取图片资源
			return images;
		} else {
			System.out
					.println("MyBitmapFactory-->getBitmaps-->images read by read dir"
							+ packagename);
			// 如果沒有则在去读取图片
			createBitmap(packagename);
			// createBitmap(packagename, "001", 55);
			return this.sequencebitmaps;
		}

	}

	public Bitmap[] getImageByCache(String imagekind) {
		// 如果缓存过就从缓存中取出数据
		if (imageCacheMap.containsKey(imagekind)) {
			SoftReference<Bitmap[]> softReference = imageCacheMap
					.get(imagekind);
			if (softReference.get() != null) {
				return softReference.get();
			}
		}
		return null;
	}

	private void closeInputStream(InputStream in) {
		if (in != null) {
			try {
				in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
